﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            double sum = 1.0; // 初始项为 1，即 1/1
            int n = 1; // 总项数，初始为 1
            int diff = 1; // 分母的差值，初始为 1
            int denominator = 1; // 初始分母
            string expression = "1"; // 表达式初始为第一项

            // 记录前10项的表达式，用于显示
            string firstTenTerms = "1";
            int termCountForDisplay = 1;

            while (true)
            {
                diff++; // 差值每次加1
                denominator += diff; // 计算下一个分母
                double currentTerm = 1.0 / denominator; // 当前项的值

                // 记录前10项的表达式
                if (termCountForDisplay <= 10)
                {
                    firstTenTerms += " + 1/" + denominator;
                    termCountForDisplay++;
                }

                sum += currentTerm; // 累加
                n++; // 项数加1

                // 当当前项的值小于1e-4时，结束循环
                if (currentTerm < 1e-4)
                {
                    break;
                }
            }

            // 处理表达式显示，只显示前10项然后加“...”
            if (termCountForDisplay > 10)
            {
                firstTenTerms += " + ...";
            }
            txtExpression.Text = firstTenTerms;
            txtResult.Text = sum.ToString();
            txtN.Text = n.ToString();
        }
    }
}
